package Forms;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import Logic.DatabaseReader;

public class TripDetailsFrame extends JFrame {

	private JPanel contentPane;
	private JTable availableFlightsTable;
	
	private JButton exitButton;
	private JButton bookButton;
	
	private LinkedList<String[]> flightInformations;
	private String[] tripInformations;
	private JTextField insuranceField;
	private JSpinner luggageSpinner;
	
	private DatabaseReader databaseReader;
	private int selectedTripRow;
	
	private String userID;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//TripDetailsFrame frame = new TripDetailsFrame();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TripDetailsFrame(String userID, DatabaseReader databaseReader, int selectedRow, LinkedList<String[]> flightInformations, String[] tripInfos)
	{
		this.userID = userID;
		this.databaseReader = databaseReader;
		selectedTripRow = selectedRow;
		this.flightInformations = flightInformations;
		this.tripInformations = tripInfos;
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 520, 370);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel tripDetailsPanel = new JPanel();
		tripDetailsPanel.setBackground(Color.ORANGE);
		tripDetailsPanel.setBounds(0, 0, 365, 180);
		contentPane.add(tripDetailsPanel);
		
		JLabel departureDateLabel = new JLabel("Departure Date: ");
		JLabel arrivalDateLabel = new JLabel("Arrival Date: ");
		
		JLabel detailsAboutTheTripLabel = new JLabel("Details about the Trip: ");
		detailsAboutTheTripLabel.setForeground(Color.BLACK);
		detailsAboutTheTripLabel.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 14));
		
		JLabel departureLocationLabel = new JLabel("Departure Location:");
		JLabel arrivalLocationLabel = new JLabel("Arrival Location:");
		JLabel totalPriceLabel = new JLabel("Total Price: ");
		
		JLabel depDatOutputLabel = new JLabel(tripInformations[0]);
		JLabel depLocOutputLabel = new JLabel(tripInformations[1]);
		JLabel arrDatOutputLabel = new JLabel(tripInformations[2]);
		JLabel arrLocOutputLabel = new JLabel(tripInformations[3]);
		JLabel totPriceEmptyLabel = new JLabel(tripInformations[5]);
		
		GroupLayout gl_tripDetailsPanel = new GroupLayout(tripDetailsPanel);
		gl_tripDetailsPanel.setHorizontalGroup(
			gl_tripDetailsPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_tripDetailsPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(detailsAboutTheTripLabel)
						.addGroup(gl_tripDetailsPanel.createSequentialGroup()
							.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(departureDateLabel)
								.addComponent(departureLocationLabel)
								.addComponent(arrivalDateLabel)
								.addComponent(arrivalLocationLabel)
								.addComponent(totalPriceLabel))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(totPriceEmptyLabel)
								.addComponent(depDatOutputLabel)
								.addComponent(depLocOutputLabel)
								.addComponent(arrDatOutputLabel)
								.addComponent(arrLocOutputLabel))))
					.addContainerGap(601, Short.MAX_VALUE))
		);
		gl_tripDetailsPanel.setVerticalGroup(
			gl_tripDetailsPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_tripDetailsPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(detailsAboutTheTripLabel)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(departureDateLabel)
						.addComponent(depDatOutputLabel))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(departureLocationLabel)
						.addComponent(depLocOutputLabel))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(arrivalDateLabel)
						.addComponent(arrDatOutputLabel))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(arrivalLocationLabel)
						.addComponent(arrLocOutputLabel))
					.addGap(18)
					.addGroup(gl_tripDetailsPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(totalPriceLabel)
						.addComponent(totPriceEmptyLabel))
					.addContainerGap(22, Short.MAX_VALUE))
		);
		tripDetailsPanel.setLayout(gl_tripDetailsPanel);
		
		JPanel mainPanel = new JPanel();
		mainPanel.setBackground(Color.WHITE);
		mainPanel.setBounds(0, 179, 800, 421);
		contentPane.add(mainPanel);
		
		JScrollPane availableFlightsScrollPane = new JScrollPane();
		
		exitButton = new JButton("Exit");
		exitButton.addActionListener(new ExitButtonListener());
		
		GroupLayout gl_mainPanel = new GroupLayout(mainPanel);
		gl_mainPanel.setHorizontalGroup(
			gl_mainPanel.createParallelGroup(Alignment.LEADING)
				.addComponent(availableFlightsScrollPane, GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
				.addGroup(Alignment.TRAILING, gl_mainPanel.createSequentialGroup()
					.addContainerGap(671, Short.MAX_VALUE)
					.addComponent(exitButton)
					.addContainerGap())
		);
		gl_mainPanel.setVerticalGroup(
			gl_mainPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_mainPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(availableFlightsScrollPane, GroupLayout.PREFERRED_SIZE, 371, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(exitButton)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		availableFlightsTable = new JTable();
		availableFlightsTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"New column", "New column", "New column", "New column", "New column", "New column"
			}
		));
		availableFlightsScrollPane.setViewportView(availableFlightsTable);
		availableFlightsTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Flightnumber", "Departure Date", "Departure Location", "Arrival Date", "Arrival Location", "Price"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		mainPanel.setLayout(gl_mainPanel);
		
		JPanel bookPanel = new JPanel();
		bookPanel.setBackground(new Color(255, 153, 51));
		bookPanel.setBounds(364, 0, 436, 180);
		contentPane.add(bookPanel);
		
		JLabel insuranceLabel = new JLabel("Insurance: ");
		
		JLabel luggageLabel = new JLabel("Luggage:");
		
		insuranceField = new JTextField();
		insuranceField.setColumns(10);
		
		bookButton = new JButton("Book");
		bookButton.addActionListener(new BookButtonListener());
		
		luggageSpinner = new JSpinner();
		luggageSpinner.setModel(new SpinnerNumberModel(0, 0, 10, 1));
		
		JLabel personsLabel = new JLabel("Persons: ");
		
		JSpinner personsSpinner = new JSpinner();
		personsSpinner.setModel(new SpinnerNumberModel(1, 1, 30, 1));
		GroupLayout gl_bookPanel = new GroupLayout(bookPanel);
		gl_bookPanel.setHorizontalGroup(
			gl_bookPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookPanel.createSequentialGroup()
					.addContainerGap(355, Short.MAX_VALUE)
					.addComponent(bookButton)
					.addContainerGap())
				.addGroup(gl_bookPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_bookPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(insuranceLabel)
						.addComponent(luggageLabel)
						.addComponent(personsLabel))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_bookPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(insuranceField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_bookPanel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(personsSpinner, Alignment.LEADING)
							.addComponent(luggageSpinner, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)))
					.addContainerGap(219, Short.MAX_VALUE))
		);
		gl_bookPanel.setVerticalGroup(
			gl_bookPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookPanel.createSequentialGroup()
					.addGap(38)
					.addGroup(gl_bookPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(insuranceLabel)
						.addComponent(insuranceField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_bookPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(luggageLabel)
						.addComponent(luggageSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_bookPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(personsLabel)
						.addComponent(personsSpinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
					.addComponent(bookButton)
					.addContainerGap())
		);
		bookPanel.setLayout(gl_bookPanel);
		this.setSize(800, 600);
		this.setResizable(false);
		this.setVisible(true);
		
		fillTable();
	}
	
	/**
	 * Fills the JTable with the informations about the flights contained in the selected trip.
	 */
	private void fillTable()
	{
		DefaultTableModel model = (DefaultTableModel) availableFlightsTable.getModel();
		
		for (String[] str : flightInformations)
		{
			model.addRow(str);
		}
	}
	
	class ExitButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			dispose();
		}	
	}
	
	class BookButtonListener implements ActionListener
	{
		/**
		 * Writes the passed informations which are needed to book a trip to the DatabaseReader-class
		 * which writes it into the database.
		 */
		public void actionPerformed(ActionEvent e)
		{
			databaseReader.writeInDatabase(insuranceField.getText(), (int) luggageSpinner.getValue(), 
					userID, selectedTripRow);
			
			JOptionPane.showMessageDialog(null,"Booking was successful, thank you and have a good journey",
					"Successful Booking", JOptionPane.PLAIN_MESSAGE);
		}
	}
}
